package com.sopt.friends.sub;

import java.util.ArrayList;

import com.sopt.friends.ChatActivity;
import com.sopt.friends.R;
import com.sopt.friends.statics.FriendsStaticVars;
import com.sopt.friends.util.FriendsContactsManager;
import com.sopt.friends.util.FriendsSms;
import com.sopt.friends.vo.FriendsBasicMember;
import com.sopt.view.HomeMemberAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class TabContentsActivity extends Activity {
	private FriendsContactsManager manager;
	private ArrayList<FriendsBasicMember> customList;
	private ListView ListView01;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabcontent);
		
		View curLayout = findViewById(R.id.tab_list_layout);
		
		ListView01 = (ListView) curLayout.findViewById(R.id.listFirst);

		ListView01.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				final TextView numberView = (TextView) view.findViewById(R.id.homeMemberRowNumber);
				
				AlertDialog listdialog = new AlertDialog.Builder(
						TabContentsActivity.this).setTitle(
						"To "+numberView.getText()).setItems(R.array.member_menu,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								switch (which) {
								case 0:
									Intent i = new Intent(TabContentsActivity.this, FriendsSms.class).setAction(numberView.getText().toString());
									startActivity(i);
									break;
								case 1:
									startActivity(new Intent(
											Intent.ACTION_CALL, Uri
													.parse("tel:"+ numberView.getText())));
									break;
								case 2:
									Intent i2 = new Intent(TabContentsActivity.this, ChatActivity.class).setAction(numberView.getText().toString());
									startActivity(i2);
									break;
								}
							}
						}).create();
				listdialog.show();
			}			
		});
		
		if (manager == null)
			manager = new FriendsContactsManager(this, getContentResolver());
		
	}

	private void setList(String currentAction,String currentTab) {
		HomeMemberAdapter mCustomAdapter = null;
		int currentRowLayout = R.layout.row_member_layout;
		
		if(currentAction.equals(String.valueOf(FriendsStaticVars.ACTION_FIRST_TO_FRESH))){
			customList = manager.getFreshList(currentTab);
			currentRowLayout = R.layout.row_member_regdate_layout;
		}else if(currentAction.equals(String.valueOf(FriendsStaticVars.ACTION_FIRST_TO_BIRTH)))
			customList = manager.getBirthList(currentTab);
		else if(currentAction.equals(String.valueOf(FriendsStaticVars.ACTION_FIRST_TO_NEWS)))
			customList = manager.getNewsList(currentTab);
		else if(currentAction.equals(String.valueOf(FriendsStaticVars.ACTION_FIRST_TO_BEST))){
			customList = manager.getBestList(currentTab);
			currentRowLayout = R.layout.row_member_ranking_number_layout;
		}else if(currentAction.equals(String.valueOf(FriendsStaticVars.ACTION_FIRST_TO_RECENTLY))){
			customList = manager.getRecentlyList(currentTab);
			currentRowLayout = R.layout.row_member_ranking_recent_layout;
		}else if(currentAction.equals(String.valueOf(FriendsStaticVars.ACTION_FIRST_TO_FORGOTTON))){
			customList = manager.getForgottonList(currentTab);
			currentRowLayout = R.layout.row_member_ranking_forgotton_layout;
		}
		
		
		
		
		mCustomAdapter = new HomeMemberAdapter(this,
				currentRowLayout, customList);
		
		if (mCustomAdapter != null)
			ListView01.setAdapter(mCustomAdapter);
	}

	@Override
	protected void onResume() {
		super.onResume();

		String request = getIntent().getAction();
		String[] req = request.split("@");
		if(req.length==2){
			setList(req[0],req[1]);
		}
	}

}
